package ciyuan.Block.Machine.tcjhc;

import ciyuan.Gui.ContainerRegistry;
import ciyuan.Item.ItemRegisterManager;
import ciyuan.Utils.TileEntityRegisterManager;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIntArray;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

/**
 * @author 150149
 */
public class TcjhcTileEntity extends TileEntity implements ITickableTileEntity, INamedContainerProvider {

    private final Inventory inventory = new Inventory(4);
    public ContainerProgress containerProgress = new ContainerProgress();
    private static final String CY_STONEPOT_OUT = "cystonepotout";
    private static final String CY_STONEPOT_COLD = "cystonepotcold";

    public TcjhcTileEntity() {
        super(TileEntityRegisterManager.tcjhc.get());
    }

    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent("ciyuan.container.dsc19");
    }

    @Nullable
    @Override
    public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new TcjhcContainer(ContainerRegistry.tcjhcContainer.get(), sycID, inventory, getBlockPos());
    }

    @Override
    public void load(BlockState state, CompoundNBT nbt) {
        this.inventory.setItem(0,ItemStack.of(nbt.getCompound("item")));
        this.inventory.setItem(1,ItemStack.of(nbt.getCompound("item2")));
        this.inventory.setItem(2,ItemStack.of(nbt.getCompound("item3")));
        this.inventory.setItem(3,ItemStack.of(nbt.getCompound("item4")));
        super.load(state, nbt);
    }

    @Override
    public CompoundNBT save(CompoundNBT compound) {
        ItemStack itemStack = this.inventory.getItem(0).copy();
        ItemStack itemStack2 = this.inventory.getItem(1).copy();
        ItemStack itemStack3 = this.inventory.getItem(2).copy();
        ItemStack itemStack4 = this.inventory.getItem(3).copy();
        compound.put("item", itemStack.serializeNBT());
        compound.put("item2", itemStack2.serializeNBT());
        compound.put("item3", itemStack3.serializeNBT());
        compound.put("item4", itemStack4.serializeNBT());
        return super.save(compound);
    }

    public Inventory getInventory() {
        return inventory;
    }

    @Override
    public void tick() {
        if (!this.hasLevel()) {
            return;
        }
        this.containerProgress.set(1,0);
        if (!this.inventory.getItem(0).isEmpty() && !this.inventory.getItem(1).isEmpty() && !this.inventory.getItem(2).isEmpty()) {
            if (this.inventory.getItem(0).getItem().getDescriptionId().equals(ItemRegisterManager.stonepot.get().getDescriptionId())) {
                if (this.inventory.getItem(2).getItem().getDescriptionId().equals(ItemRegisterManager.sulphuricacid.get().getDescriptionId())) {
                    String itemName = this.inventory.getItem(1).getItem().getDescriptionId();
                    String out = null;
                    if (itemName.equals(ItemRegisterManager.broken_aluminium.get().getDescriptionId())) {
                        out = ItemRegisterManager.Aluminium.get().getDescriptionId();
                    } else if (itemName.equals(ItemRegisterManager.broken_copper.get().getDescriptionId())) {
                        out = ItemRegisterManager.Copper.get().getDescriptionId();
                    } else if (itemName.equals(ItemRegisterManager.broken_gold.get().getDescriptionId())) {
                        out = Items.GOLD_INGOT.getDescriptionId();
                    } else if (itemName.equals(ItemRegisterManager.broken_iron.get().getDescriptionId())) {
                        out = Items.IRON_INGOT.getDescriptionId();
                    } else if (itemName.equals(ItemRegisterManager.broken_tin.get().getDescriptionId())) {
                        out = ItemRegisterManager.tin.get().getDescriptionId();
                    }

                    if (out==null) {
                        return;
                    }

                    if (this.containerProgress.get(0)>=800) {
                        ItemStack itemStack = new ItemStack(ItemRegisterManager.stonepot1.get());
                        CompoundNBT compoundNBT = new CompoundNBT();
                        compoundNBT.putString(CY_STONEPOT_OUT,out);
                        itemStack.setTag(compoundNBT);
                        if (this.inventory.canAddItem(itemStack)) {
                            this.inventory.getItem(0).setCount(this.inventory.getItem(0).getCount()-1);
                            this.inventory.getItem(1).setCount(this.inventory.getItem(1).getCount()-1);
                            this.inventory.getItem(2).setCount(this.inventory.getItem(2).getCount()-1);
                            this.containerProgress.set(0,0);
                            this.inventory.addItem(itemStack);
                        }
                        if (this.inventory.canAddItem(itemStack)) {
                            this.inventory.getItem(0).setCount(this.inventory.getItem(0).getCount()-1);
                            this.inventory.getItem(2).setCount(this.inventory.getItem(2).getCount()-1);
                            this.containerProgress.set(0,0);
                            this.inventory.addItem(itemStack);
                        }
                    } else if (this.containerProgress.get(0)<800 ) {
                        this.containerProgress.set(0,this.containerProgress.get(0)+1);
                        this.containerProgress.set(1,1);
                    }
                }
            }
        }
    }

    public class ContainerProgress implements IIntArray {

        int[] progress = {0,0,0};

        @Override
        public int get(int index) {
            return progress[index];
        }

        @Override
        public void set(int index, int value) {
            progress[index] = value;
        }

        @Override
        public int getCount() {
            return 2;
        }
    }
}
